package user_assets_ewallet

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/emun"
	xerrors "gitee.com/liuxuezhan/ar-platform/admin-api/pkg/xerrors/assets"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserEWalletListPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 分页查询
func NewUserEWalletListPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserEWalletListPageLogic {
	return &UserEWalletListPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserEWalletListPageLogic) UserEWalletListPage(req *types.UserEWalletsPageRequest) (resp *types.UserEWalletsPageResponse, err error) {
	request := rpc.UserEWalletsPageRequest{}
	err = copier.Copy(&request, req)
	if req.Param.WalletName == 0 {
		request.Param.WalletName = ""
	} else {
		request.Param.WalletName = strconv.FormatInt(int64(req.Param.WalletName), 10)
	}

	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	page, err := l.svcCtx.AssetsRpc.UserChannel.UserEWalletsPage(l.ctx, &request)
	response := types.UserEWalletsPageResponse{}
	if err != nil {
		return &response, err
	}
	err = copier.Copy(&response, page)
	for i := 0; i < len(page.DataList); i++ {
		key, err := strconv.ParseInt(page.DataList[i].WalletName, 10, 64)
		if err != nil {
			return &response, xerrors.ErrParse
		}
		response.DataList[i].WalletName = emun.GetUserEWalletNameByType(int(key))
	}

	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	return &response, nil
}
